<?php
class SignupController extends Zend_Controller_Action {
	public function indexAction() {
		$this->view->notloggedin = true;
		
		if (isset($_POST['username'])) {
			/* Validate form */
			$validators = array(
				'username' => 'Alnum',
				'otp' => array(
					'Alnum',
					new Zend_Validate_StringLength(13, 128),
				),
				'password' => array('presence' => 'required'),
				'apiid' => 'Digits',
				'sigkey' => array('presence' => 'required'),
				
			);
			$input = new Zend_Filter_Input(NULL, $validators, $_POST);
			if ($input->hasInvalid() || $input->hasMissing()) {
				$this->view->message = $input->getMessages();
				
				// Set vars for view
				$this->view->username = $input->username;
				$this->view->password = $input->password;
				$this->view->apiid = $input->apiid;
				$this->view->sigkey = $input->sigkey;
			}
			elseif (is_array(NerdPass_Data::getUser($_POST['username']))) {
				$this->view->user_exists = true;
				// Set vars for view
				$this->view->username = $input->username;
				$this->view->password = $input->password;
				$this->view->apiid = $input->apiid;
				$this->view->sigkey = $input->sigkey;
			}
			else {
				NerdPass_Data::addUser($_POST['username'], $_POST['password'], $_POST['apiid'], $_POST['sigkey'], $_POST['otp'], 'user');
				$this->view->success = "You have been added successfully! Please login using your username, password, and Yubikey.";
			}
		}
	}
}